
var ctx = "http://localhost:8180/jquery-easyui-1.4.5/";

var result = [{"id":"2016002","resName":"门诊管理"},{"id":"2016003","resName":"报销管理"},{"id":"2016004","resName":"库存管理"},{"id":"2016005","resName":"系统管理"}];
var result2 = [{"id":"sub2016002","resName":"门诊记录","resUrl":"demo/datagridview/datagrid-bufferview.html"},{"id":"sub2016003","resName":"门诊收费","resUrl":""},{"id":"sub2016004","resName":"患者登记","resUrl":""}];

var menu_parent = null, menu_child = null;
var original_bgcolor = "#4682b4", click_bgcolor = "#FF6347";

$(function(){
	
	//记下一级菜单的第一个元素.
	var remember_first_menu_1 = null;
	remember_first_menu_1 = loadParentMenu(result);
	
	//触发一级菜单第一个元素的点击事件.
	if (remember_first_menu_1) {
		remember_first_menu_1.trigger('click');
	}
});

function loadParentMenu(result){
	if (result) {
		menu_parentArr = result;
		//加载一级菜单{begin}
		if (menu_parentArr && menu_parentArr.length>0) {
			$.each(menu_parentArr, function(index, obj){
				var new_li = $('<li>').appendTo($('#id_menu01'));
				var new_a = $('<a>').attr('id', 'first_'+obj['id'])
									.attr('href', 'javascript:void(0)')
									.addClass('menu01-a-style')
									.html(obj['resName'])
									.appendTo(new_li);
				if (index == 0) {
					remember_first_menu_1 = new_a;
				}
				new_a.on('click', function(){
					//这里写"$('#id_menu01').find('a')"也是可以的,但是为避免引用具体的具名ID采用了如下方式.
					new_a.parents('div').find('a[class*="a-clicked"]').removeClass("a-clicked");
					new_a.addClass("a-clicked");

					var parentId = obj['id'];
					//加载子菜单 -> 这里是从服务器上获取,在菜单不变的生产环境下需要优化(直接使用'menu_childArr').
					loadChildMenu(result2);
				});
			});
		}
		//加载一级菜单{end}
	}
	return remember_first_menu_1;
}

function loadChildMenu(result2){
	if (result2 && result2.length>0){
		//加载二级菜单.
		var second_menu_parent = $("#id_menu02");
		second_menu_parent.empty();
		$.each(result2, function(index2, obj2){
			var new_a2 = $('<a>').attr('id', 'second_'+obj2['id'])
					.attr('href', 'javascript:void(0)')
					.addClass('cs02')
					.html(obj2['resName']).appendTo(second_menu_parent);
			var mainIframeUrl = ctx + obj2['resUrl'];
			new_a2.on('click', function(){
				$.each(new_a2.siblings(), function(i, n){
					$(n).removeClass("cs02-clicked");
				});
				new_a2.addClass("cs02-clicked");
				$("#mainIframe").attr('src', mainIframeUrl);
			});
		});
	}
}

